import { BasicColumn } from "/@/components/Table"
import { FormSchema } from "/@/components/Table"
import { rules } from "/@/utils/helper/validator"
import { render } from "/@/utils/common/renderUtils"
import { UploadBizType } from "/@/enums/uploadEnum"
import { AvailableEnum } from '/@/enums'
import * as postApi from '/@/api/iam/post'
import { DescItem } from "/@/components/Description/index"
import * as roleApi from '/@/api/iam/role'
import * as deptApi from '/@/api/iam/dept'
import { h } from 'vue'
import Copy from "/@/components/My/Copy.vue"
export const TAB_TYPE = {
  BASE_CONFIG: "baseConfig",
  PROTOCOL_CONFIG: "protocolConfig",
  ASSIGN_PERIMSIONS: "assignPerimsions",
  LOGIN_CONTROLL: "loginControll",
  SECRET_CONFIG: "secretConfig",
}

export enum PageUrl {
  APP_LIST = "/console/microApp",
}

export const columns: BasicColumn[] = [
  {
    title: "ID",
    dataIndex: "id",
    width: 150,
  },
  {
    title: "图标",
    dataIndex: "logo",
    width: 80,
    customRender: ({ text }) => {
      return render.renderAvatar(text || "/@/assets/images/default-app.png")
    },
  },

  {
    title: "应用名",
    dataIndex: "name",
    width: 150,
  },
  {
    title: "说明",
    dataIndex: "remark",
    width: 150,
    customRender: ({ text }) => {
      return text ? text : '-'
    },
  },
  {
    title: "创建时间",
    dataIndex: "createdAt",
    width: 150,
  },
  {
    title: "状态",
    dataIndex: "status",
    customRender: ({ text }) => {
      let color = "green"
      if (text === "2") {
        color = "red"
      }
      return render.renderTag(render.renderDict(text, "user_status"), color)
    },
    width: 60,
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: "名称",
    field: "name",
    component: "Input",
  },
  {
    label: "状态",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      allowClear: true,
      dictType: "user_status",
      placeholder: "全部",
      stringToNumber: true,
    },
  },
  {
    label: "应用类型",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      allowClear: true,
      dictType: "user_status",
      placeholder: "请选择状态",
      stringToNumber: true,
    },
  },
]

export const formSchema: FormSchema[] = [
  {
    label: "",
    field: "userId",
    component: "Input",
    show: false,
  },
  {
    label: "用户名称",
    field: "userName",
    component: "Input",
    dynamicDisabled: ({ }) => true,
    colProps: { span: 8 }
  },
  {
    label: "用户昵称",
    field: "nickName",
    component: "Input",
    colProps: { span: 8 },
  },
  {
    label: "邮箱",
    field: "email",
    component: "Input",
    rules: rules.rule("email", false),
    colProps: { span: 8 },
    dynamicRules: ({ model, schema }) => {
      if (!model.id || (model.id && model.email?.indexOf("*") !== -1)) {
        return [{ required: false }]
      }
      return rules.rule("email", false)
    },
  },
  {
    label: "手机号码",
    field: "phone",
    component: "Input",
    colProps: { span: 8 },
    dynamicRules: ({ model, schema }) => {
      if (!model.phone || (model.id && model.phone?.indexOf("*") !== -1)) {
        return [{ required: false }]
      }
      return [{ pattern: /^1[3|4|5|7|8|9][0-9]\d{8}$/, message: "手机号码格式有误" }]
    },
  },
  {
    label: "工号",
    field: "workNo",
    component: "Input",
    colProps: { span: 8 },
  },

  {
    label: "角色",
    field: "roleIds",
    required: true,
    component: "ApiSelect",
    colProps: { span: 8 },
    componentProps: {
      mode: "multiple",
      api: roleApi.listRole,
      labelField: "roleName",
      valueField: "id",
    },
  },
  {
    label: "岗位",
    field: "postIds",
    required: false,
    defaultValue: [],
    colProps: { span: 8 },
    component: "ApiSelect",
    componentProps: {
      mode: "multiple",
      api: postApi.listPost,
      labelField: "name",
      valueField: "id",
    },
  },
  {
    label: '部门',
    required: false,
    field: 'deptId',
    component: "MyTreeSelect",
    colProps: { span: 8 },
    componentProps: {
      immediate: true,
      api: () => deptApi.listDeptTree({ status: AvailableEnum.NORMAL }),
      placeholder: "请选择",
      fieldNames: {
        label: "deptName",
        pid: "parentId",
        key: "id",
        value: "id",
      },
    },
  },
  {
    label: "生日",
    field: "birthday",
    colProps: { span: 8 },
    component: "DatePicker",
  },
  {
    label: "性别",
    field: "sex",
    colProps: { span: 8 },
    component: "JDictSelectTag",
    componentProps: {
      dictType: "sys_user_sex",
      placeholder: "请选择",
      // stringToNumber: true
    },
  },
  {
    label: " ",
    itemProps: {
      colon: false
    },
    field: "footer-action",
    component: "Input",
    slot: 'footer-action'
  },
]


/**
 * 协议配置
 */
export const protocolFormSchema: FormSchema[] = [
  {
    label: "",
    field: "divider-1",
    component: "Input",
    render: () => {
      return render.renderDividerWithValue("基础配置", { orientation: 'left' })
    },
    span: 2,
  },
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    label: "默认协议",
    field: "protocol",
    required: true,
    component: "JDictSelectTag",
    helpMessage: '当直接访问认证地址时默认使用的协议。禁用的协议需要在下方「授权配置」中开启并保存后方可启用。',
    defaultValue: "oidc",
    itemProps: {
      colon: true
    },
    componentProps: {
      type: "radio",
      dictType: "AuthProtocolType",
    },
  },

  {
    label: "",
    field: "divider-2",
    component: "Input",
    render: () => {
      return render.renderDividerWithValue("授权配置", { orientation: 'left' })
    },
    span: 2,
  },

  {
    label: "授权模式",
    field: "oidcConfig_grantTypes",
    required: true,
    component: "JDictSelectTag",
    helpMessage: '如果开启 implicit，请确保回调地址全部为 https',
    defaultValue: [],
    componentProps: {
      type: "select",
      mode: "multiple",
      dictType: "GrantTypes",
    },
  },
  {
    label: "返回类型",
    field: 'oidcConfig_responseTypes',
    required: true,
    defaultValue: ['code'],
    component: "JDictSelectTag",
    helpMessage: '配置 Iam 支持从授权端点返回的数据项，code 代表支持返回授权码，token 代表支持返回 Access token，id_token 代表支持返回 Id token，但如果发起请求时不主动向 Iam 索取，Iam 不会主动返回相应数据',
    componentProps: {
      type: "select",
      mode: "multiple",
      dictType: "AuthResourceType",
    },
  },
  {
    label: "授权码过期时间",
    field: "authorizationCodeExpire",
    required: true,
    slot: 'authorizationCodeExpire',
    component: "Input",
    helpMessage: '兑换 token 的授权码过期时间（建议 600 秒），若在有效期内未使用，则导致所有身份或资源凭证无法兑换，最终无法进行登录。',
    defaultValue: "600"
  },
  {
    label: "id_token 过期时间",
    field: "idTokenExpire",
    required: true,
    slot: 'idTokenExpire',
    component: "Input",
    helpMessage: 'Id_token 是指用户的身份凭证（建议 1209600 秒/ 14 天），过期后用户的身份信息无法认证，需要重新登录，若需要延长登录持续时间，可以修改该配置',
    defaultValue: "600"
  },
  {
    label: "access_token 过期时间",
    field: "accessTokenExpire",
    required: true,
    slot: 'accessTokenExpire',
    component: "Input",
    helpMessage: 'access_token 是允许访问资源的凭证（建议 1209600 秒/ 14 天），过期后用户的权限资源无法认证，需要重新登录',
    defaultValue: "600"
  },
  {
    label: "refresh_token 过期时间",
    field: "refreshTokenExpire",
    slot: 'refreshTokenExpire',
    required: true,
    component: "Input",
    helpMessage: 'refresh_token 是获取新的 access_token、id_token 的凭证（建议 2592000 秒/ 30 天，该配置一般比 access_token 要长），过期后无法刷新 id_token 和 access_token，最终会导致用户需要重新登录',
    defaultValue: "600"
  },
  {
    label: "",
    field: "divider-3",
    component: "Input",
    render: () => {
      return render.renderDividerWithValue("配置信息", { orientation: 'left' })
    },
    span: 2,
  },

  {
    label: " ",
    itemProps: {
      colon: false
    },
    field: "footer-action",
    component: "Input",
    slot: 'footer-action'
  },
]



export const newAppFormSchema: FormSchema[] = [
  {
    label: "",
    field: "id",
    show: false,
    component: "Input",
  },
  {
    label: "应用名称",
    field: "name",
    required: true,
    component: "Input"
  },
  {
    label: "应用分组",
    field: "groupId",
    required: false,
    component: "Input"
  },
  {
    field: "remark",
    label: "备注",
    component: "InputTextArea",
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 8,
      },
    },
  },

]


export const baseInfoDescSchema: DescItem[] = [
  {
    field: "userName",
    label: "用户名称",
    span: 1,
    labelMinWidth: 60,
  },
  {
    field: "userId",
    label: "用户ID",
    span: 1,
    labelMinWidth: 50,
    contentMinWidth: 160,
    render: (curVal, data) => {
      return h(Copy, { value: curVal })
    },
  },
  {
    field: "lastLoginTime",
    label: "最后登录时间",
    span: 1,
    labelMinWidth: 85,
    contentMinWidth: 160
  },
  {
    field: "lastLoginIp",
    label: "最后登录IP",
    span: 1,
    labelMinWidth: 70,
    contentMinWidth: 160
  },
  {
    field: "createdAt",
    label: "加入时间",
    span: 1,
    labelMinWidth: 60,
    contentMinWidth: 160
  }
]

